<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestBaseTopic extends PHPUnit_Framework_TestCase {
	
	function test_get_type() {
		$expected_item_type_id = 5;
		$expected_post_item_type_id = 7;

		$_POST['item_type_id'] = $expected_post_item_type_id;

		$actual = BaseTopic::get_type(false, array('item_type_id' => $expected_item_type_id));

		$this->assertEquals($expected_item_type_id, $actual);
	}

	function test_get_type_with_new() {
		$expected_item_type_id = 5;
		$expected_post_item_type_id = 7;

		$_POST['item_type_id'] = $expected_post_item_type_id;

		$actual = BaseTopic::get_type(true, array('item_type_id' => $expected_item_type_id));

		$this->assertEquals($expected_post_item_type_id, $actual);
	}

	function test_new_topic_topic() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Topic));

		$this->assertType('Topic', $actual);
	}

	function test_new_topic_article() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Article));

		$this->assertType('Article', $actual);
	}

	function test_new_topic_link() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::WebLink));

		$this->assertType('WebLink', $actual);
	}

	function test_new_topic_download() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Download));

		$this->assertType('Download', $actual);
	}

	function test_new_topic_poll() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Poll));

		$this->assertType('Poll', $actual);
	}

	function test_new_topic_news() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::News));

		$this->assertType('News', $actual);
	}

	function test_new_topic_fiche() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Fiche));

		$this->assertType('Fiche', $actual);
	}

	function test_new_topic_event() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Event));

		$this->assertType('Event', $actual);
	}

	function test_new_topic_gallery() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Gallery));

		$this->assertType('Gallery', $actual);
	}

	function test_new_topic_ad() {
		$page = new Page();
		$actual = BaseTopic::new_topic($page, false, array('item_type_id' => ItemType::Ad));

		$this->assertType('Ad', $actual);
	}

	function test_get_instance_topic() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "topic", 0);

		$this->assertType('Topic', $actual);
	}

	function test_get_instance_article() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "article", 0);

		$this->assertType('Article', $actual);
	}

	function test_get_instance_download() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "download", 0);

		$this->assertType('Download', $actual);
	}

	function test_get_instance_poll() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "poll", 0);

		$this->assertType('Poll', $actual);
	}

	function test_get_instance_news() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "news", 0);

		$this->assertType('News', $actual);
	}

	function test_get_instance_gallery() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "gallery", 0);

		$this->assertType('Gallery', $actual);
	}

	function test_get_instance_event() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "event", 0);

		$this->assertType('Event', $actual);
	}

	function test_get_instance_fiche() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "fiche", 0);

		$this->assertType('Fiche', $actual);
	}

	function test_get_instance_ad() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "ad", 0);

		$this->assertType('Ad', $actual);
	}

	function test_get_instance_weblink() {
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "link", 0);

		$this->assertType('WebLink', $actual);
	}

	function test_get_instance_unknown() {
		$this->setExpectedException('UnknownItemTypeException');
		$page = new Page();
		$actual = BaseTopic::get_instance($page, "supercalifradgilistiexpialidocious", 0);
	}

	function test_create_new() {

		$expected_title = 'title_param';
		$expected_info = 'info_param';
		$expected_sqlized_title = 'sqlized_title';
		$expected_sqlized_info = 'sqlized_info';
		$expected_session_user_id = 'session_user_id';
		$expected_post_item_type_id = 5;
		$expected_insert_query = 'insert_query';
		$expected_insert_id = 2548;
		$expected_post_item_type_id = 4586;
		$expected_session_user_id = 3698;
		$expected_sql_from_post = 'sql_from_post';
		$expected_insert_return_value = 'insert_return_value';
		$expected_post_new_subset = "new_subset";
		$expected_sqlized_new_subset = "sqlized_new_subset";
		$expected_subset_query = "subset_query";
		$expected_subset_insert_id = 2547;
		$expected_subset_hierarchy_query = "subset_hierarchy_query";

		$_POST['item_type_id'] = $expected_post_item_type_id;
		$_SESSION['user_id'] = $expected_session_user_id;

		$page = new Page();
		$base_topic = new BaseTopicTest($page);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->at(0))
				->method('db_sqlize')
				->with($expected_title)
				->will($this->returnValue($expected_sqlized_title));
		$mock_db_access->expects($this->at(1))
				->method('db_sqlize')
				->with($expected_info)
				->will($this->returnValue($expected_sqlized_info));
		$mock_db_access->expects($this->at(2))
				->method('db_query_insert')
				->with(array("item_type_id", "author", "editor", "title", "info", "hits", 
								"creation", "edit", "num_posts"), 
						array($expected_post_item_type_id, 
								$expected_session_user_id, 
								$expected_session_user_id,
								$expected_sqlized_title,
								$expected_sqlized_info, 
								0, 
								"NOW(14)", 
								"NOW(14)", 
								0), 
						"ITEM")
				->will($this->returnValue($expected_insert_query));
		$mock_db_access->expects($this->at(3))
				->method('db_do_query')
				->with($expected_insert_query);
		$mock_db_access->expects($this->at(4))
				->method('db_insert_id')
				->with()
				->will($this->returnValue($expected_insert_id));
		$mock_db_access->expects($this->at(5))
				->method('db_do_query')
				->with($expected_sql_from_post)
				->will($this->returnValue($expected_insert_return_value));

		$base_topic->set_db_accessor($mock_db_access);
		$base_topic->set_derive_info_result($expected_info);
		$base_topic->set_derive_title_result($expected_title);
		$base_topic->set_get_sql_from_post_result($expected_sql_from_post);

		$base_topic->create_new();
		$this->markTestIncomplete();
	}

	function test_create_new_with_new_subset() {

		$expected_title = 'title_param';
		$expected_info = 'info_param';
		$expected_sqlized_title = 'sqlized_title';
		$expected_sqlized_info = 'sqlized_info';
		$expected_session_user_id = 'session_user_id';
		$expected_post_item_type_id = 5;
		$expected_insert_query = 'insert_query';
		$expected_insert_id = 2548;
		$expected_post_item_type_id = 4586;
		$expected_session_user_id = 3698;
		$expected_sql_from_post = 'sql_from_post';
		$expected_insert_return_value = 'insert_return_value';
		$expected_post_new_subset = "new_subset";
		$expected_sqlized_new_subset = "sqlized_new_subset";
		$expected_subset_query = "subset_query";
		$expected_subset_insert_id = 2547;
		$expected_subset_hierarchy_query = "subset_hierarchy_query";
		$expected_sub_id = 'NEW';
		$expected_post_new_subset = 'post_new_subset';
		$expected_sqlized_post_new_subset = 'sqlized_post_new_subset';
		$expected_subset_insert_query = 'subset_insert_query';
		$expected_insert_id = 4568;
		$expected_this_dir_value = 'this_dir_value';
		$expected_this_dir = array("dir_id" => $expected_this_dir_value);
		$expected_sub_in_insert_query = 'sub_in_insert_query';

		$_POST['new_subset'] = $expected_post_new_subset;
		$_POST['sub_id'] = $expected_sub_id;
		$_POST['item_type_id'] = $expected_post_item_type_id;
		$_SESSION['user_id'] = $expected_session_user_id;

		$page = new Page();
		$page->dir_depth = 3;
		$page->directory_tree[$page->dir_depth - 1] = $expected_this_dir;
		$base_topic = new BaseTopicTest($page);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->at(0))
				->method('db_sqlize')
				->with($expected_title)
				->will($this->returnValue($expected_sqlized_title));
		$mock_db_access->expects($this->at(1))
				->method('db_sqlize')
				->with($expected_info)
				->will($this->returnValue($expected_sqlized_info));
		$mock_db_access->expects($this->at(2))
				->method('db_query_insert')
				->with(array("item_type_id", "author", "editor", "title", "info", "hits", 
								"creation", "edit", "num_posts"), 
						array($expected_post_item_type_id, 
								$expected_session_user_id, 
								$expected_session_user_id,
								$expected_sqlized_title,
								$expected_sqlized_info, 
								0, 
								"NOW(14)", 
								"NOW(14)", 
								0), 
						"ITEM")
				->will($this->returnValue($expected_insert_query));
		$mock_db_access->expects($this->at(3))
				->method('db_do_query')
				->with($expected_insert_query);
		$mock_db_access->expects($this->at(4))
				->method('db_insert_id')
				->with()
				->will($this->returnValue($expected_insert_id));
		$mock_db_access->expects($this->at(5))
				->method('db_do_query')
				->with($expected_sql_from_post)
				->will($this->returnValue($expected_insert_return_value));
		$mock_db_access->expects($this->at(6))
				->method('db_sqlize')
				->with($expected_post_new_subset)
				->will($this->returnValue($expected_sqlized_post_new_subset));
		$mock_db_access->expects($this->at(7))
				->method('db_query_insert')
				->with(array('sub_title'), 
						array($expected_sqlized_post_new_subset),
						"SUBSET")
				->will($this->returnValue($expected_subset_insert_query));
		$mock_db_access->expects($this->at(8))
				->method('db_do_query')
				->with($expected_subset_insert_query);
		$mock_db_access->expects($this->at(9))
				->method('db_insert_id')
				->with()
				->will($this->returnValue($expected_insert_id));
		$mock_db_access->expects($this->at(10))
				->method('db_sqlize')
				->with($expected_post_new_subset)
				->will($this->returnValue($expected_sqlized_post_new_subset));
		$mock_db_access->expects($this->at(11))
				->method('db_query_insert')
				->with(array('dir_id', 'sub_id', 'sub_title'), 
						array($expected_this_dir_value, 
								$expected_insert_id, 
								$expected_sqlized_post_new_subset),
						"SUB_IN")
				->will($this->returnValue($expected_sub_in_insert_query));
		$mock_db_access->expects($this->at(12))
				->method('db_do_query')
				->with($expected_sub_in_insert_query);

		$base_topic->set_db_accessor($mock_db_access);
		$base_topic->set_derive_info_result($expected_info);
		$base_topic->set_derive_title_result($expected_title);
		$base_topic->set_get_sql_from_post_result($expected_sql_from_post);

		$actual = $base_topic->create_new();
		$this->assertEquals($expeced_insert_id, $actual);
	}

	function test_get_id() {
		$page = new Page();
		$expected_id = 36589;

		$item = new BaseTopic($page, $expected_id);

		$actual = $item->get_id();

		$this->assertEquals($expected_id, $actual);
	}

	function test_derive_title() {
		$expected_title = 'expected_title';

		$_POST['title'] = $expected_title;

		$page = new Page();
		$item = new BaseTopicTest($page);
		$actual = $item->derive_title_test();
		$this->assertEquals($expected_title, $actual);
	}

	function test_derive_info() {
		$expected_info = '';
		
		$page = new Page();
		$item = new BaseTopicTest($page);
		$actual = $item->derive_info_test();

		$this->assertEquals($expected_info, $actual);
	}

	function test_get_sql_from_post() {
		$expected_sql = '';
		
		$page = new Page();
		$item = new BaseTopicTest($page);

		$actual = $item->get_sql_from_post_test();

		$this->assertEquals($expected_sql, $actual);
	}
}

class BaseTopicTest extends BaseTopic {

	private $derive_info_result;
	private $derive_title_result;
	private $get_sql_from_post_result;

	function set_get_sql_from_post_result($result) {
		$this->get_sql_from_post_result = $result;
	}

	function set_derive_info_result($result) {
		$this->derive_info_result = $result;
	}
	
	function set_derive_title_result($result) {
		$this->derive_title_result = $result;
	}

	function derive_info() {
		return $this->derive_info_result;
	}

	function derive_title() {
		return $this->derive_title_result;
	}

	function derive_title_test() {
		return parent::derive_title();
	}

	function derive_info_test() {
		return parent::derive_info();
	}

	function get_sql_from_post() {
		return $this->get_sql_from_post_result;
	}

	function get_sql_from_post_test() {
		return parent::get_sql_from_post();
	}
}
